package com.googlecode.proxjed;

import com.google.common.base.Predicate;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class NullSafeProxy<T> extends Proxy<T> {

    public NullSafeProxy(T instance, Class<T> type, Predicate<Method> joinPoint) {
        super(instance, type, joinPoint);
    }

    public Object doIntercept(T o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        try {
            return method.invoke(instance, objects);
        } catch (NullPointerException npe) {
            return null;
        } catch (InvocationTargetException ex) {
            if (ex.getTargetException() instanceof NullPointerException) {
                return null;
            }
            throw ex;
        }
    }

}
